package Eyjafjalla.action;

import Eyjafjalla.card.status.Burn_Eyjafjalla;
import Eyjafjalla.helper.EyjafjallaHelper;
import Eyjafjalla.helper.StringHelper;
import Eyjafjalla.patch.CloudPatch;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.actions.GameActionManager;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.cards.CardGroup;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;

import java.util.ArrayList;

public class DancingLavaAction extends AbstractGameAction {
    public DancingLavaAction(){
        actionType = ActionType.CARD_MANIPULATION;
        startDuration = duration = Settings.ACTION_DUR_FAST;
    }

    @Override
    public void update() {
        if(startDuration==duration){
            boolean returnAll = true;
            if(setBurn(AbstractDungeon.player.drawPile))
                returnAll = false;
            if(setBurn(AbstractDungeon.player.hand))
                returnAll = false;
            if(setBurn(CloudPatch.cloudGroup))
                returnAll = false;
            if(setBurn(AbstractDungeon.player.discardPile))
                returnAll = false;
            if(returnAll){
                this.isDone = true;
                return;
            }
        }

        this.tickDuration();
    }

    private boolean setBurn(CardGroup group){
        ArrayList<AbstractCard> status = new ArrayList<>();
        for(AbstractCard c:group.group){
            if(c.type.equals(AbstractCard.CardType.STATUS))
                status.add(c);
        }
        for(AbstractCard c:status){
            int index = group.group.indexOf(c);
            group.group.set(index,getBurn(c));
        }
        if(group.type == CardGroup.CardGroupType.HAND)
            AbstractDungeon.player.hand.refreshHandLayout();
        return !status.isEmpty();
    }

    private static AbstractCard getBurn(AbstractCard c){
        AbstractCard burn = new Burn_Eyjafjalla();
        for(int i = 0; i< GameActionManager.turn; i++)
            burn.upgrade();
        burn.untip();
        burn.current_x = c.current_x;
        burn.current_y = c.current_y;
        burn.target_x = c.target_x;
        burn.target_y = c.target_y;
        burn.angle = c.angle;
        burn.drawScale = c.drawScale;
        burn.flash();
        return burn;
    }
}
